# -*- encoding=utf8 -*-
__author__ = "yangxianming"

from airtest.core.api import *
# ST.PROJECT_ROOT = "E:\AirtestWorkSpace\fuyuanwaiUItest"
# using(r"E:\AirtestWorkSpace\fuyuanwaiUItest\common.air")
# from common import *

auto_setup(__file__)

#拿到设备编号
import sys
args = sys.argv
devicePid = args[4].split("/")[3]
print("--------" + devicePid)
rootArr = args[2].split("\\")
scriptRoot = ""
for i in range(len(rootArr) - 1):
    scriptRoot = scriptRoot + rootArr[i] + "\\"
print("--------" + scriptRoot)

#对更目录下设变编号文件做注册登记操作 1代表在使用  0代表停止使用
with open(scriptRoot + devicePid, 'w') as f:
    f.write('1')
    
using(scriptRoot + "common.air")
from common import *
stop_app("com.alibaba.android.rimet")
start_app("com.alibaba.android.rimet")

try:
    sleep(1.0)
    openKoufu()
    sleep(1.0)
    ##点进商城
    touch(Template(r"tpl1682230808189.png", record_pos=(-0.002, 0.815), resolution=(1080, 1920)))
    ##等待搜索框js加载完毕
    sleep(10.0)
    touch(Template(r"tpl1682230855402.png", record_pos=(-0.002, -0.633), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1682230875105.png", record_pos=(-0.004, -0.631), resolution=(1080, 1920)))
    sleep(1.0)
    text("牙膏")
    sleep(2.0)
    touch((280, 760))
    ##加入购物车
    touch(Template(r"tpl1682228089067.png", record_pos=(-0.231, 0.808), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1682219433943.png", record_pos=(-0.256, 0.263), resolution=(1080, 1920)))
    sleep(1.0)
    ##点击加入购物车
    touch(Template(r"tpl1682228089067.png", record_pos=(-0.231, 0.808), resolution=(1080, 1920)))
    sleep(1.0)
    #点购物车
    touch(Template(r"tpl1682228119168.png", record_pos=(-0.262, 0.814), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1682228147803.png", record_pos=(0.32, 0.825), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1682228167900.png", record_pos=(0.296, 0.806), resolution=(1080, 1920)))
    sleep(1.0)
    
    ##验证我们的收银台
    assert_exists(Template(r"tpl1682228191495.png", record_pos=(-0.276, 0.485), resolution=(1080, 1920)), "验证我们的收银台")

except Exception as e:
    print(e)
finally:
    sleep(1.0)
    stop_app("com.alibaba.android.rimet")
    #完成测试等级设备为0以便被其他脚本使用
    with open(scriptRoot + devicePid, 'w') as f:
        f.write('0')
